package com.bdcom.nms.NmsCollect.thread;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.opengoss.snmphibernate.api.ISnmpSession;

import com.bdcom.nms.NmsCollect.App;
import com.bdcom.nms.NmsCollect.pojo.IfEntry;
import com.bdcom.nms.NmsCollect.pojo.SystemInfo;

/**
 * 采集设备的实现类
 * 
 * @author Administrator
 *
 */
public class BdcomSwitchCollectThread extends Thread {

	static  Logger _log=Logger.getLogger(BdcomSwitchCollectThread.class);
	
	private ISnmpSession session;
	
	private BdcomSwitchPersistThread persistthread=(BdcomSwitchPersistThread) App.app_context.getBean("persistthread");

	private String ip;

	public BdcomSwitchCollectThread(ISnmpSession session, String ip) {
		super();
		this.session = session;
		this.ip = ip;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub

		/**
		 * 执行完采集结果，获取数据！
		 */
		try {

			Long collecttime=new Date().getTime();
			
			SystemInfo s = session.get(SystemInfo.class);
			
			s.setIp(ip);
			
			s.setCollectTime(collecttime);
			
			persistthread.execute(s);
		
			List<IfEntry> entrylist=session.getTable(IfEntry.class);
			
			for(IfEntry entry:entrylist){
				
				if(entry.getIfOperStatus()==1){
					
					entry.setIp(ip);
					
					entry.setCollectTime(collecttime);
					
					persistthread.execute(entry);
					
				}
				
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			_log.error("Collect "+ip+" Exception! :"+e.getLocalizedMessage());
			
		} finally{
			
			persistthread=null;
			
			if(session!=null){
				
				try {
					
					session.close();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
				_log.error("Close "+ip+" Session Exception! :"+e.getLocalizedMessage());
				
				}
			}
			
			session=null;
			
		}
		
	}

}
